import { Component, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { Body, _HttpClient } from '@delon/theme';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { LandlorEditComponent } from '../landlor-edit/landlor-edit.component';
import { ApiService } from '@core/services/api';
import { STData } from '@delon/abc';
import { LandlordAddComponent } from '../landlord-add/landlord-add.component';
import { sendCardComponent } from '../sendCard/sendCard.component';
import { LandlorUpdateComponent } from '../landlor-update/landlor-update.component';
import { ACLService } from '@delon/acl';

@Component({
  selector: 'landlor-list',
  templateUrl: './landlor-list.component.html',
  styles: []
})
export class LandlorListComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  addFlag:boolean = this.aclService.canAbility("landlor-manage/add");//检查用户按钮权限
  editFlag:boolean =  this.aclService.canAbility("landlor-manage/edit");
  viewFlag:boolean =  this.aclService.canAbility("landlor-manage/view");
  cardFlag:boolean =  this.aclService.canAbility("landlor-manage/sendcard");
  selectData: any[];
  loading = false;
  config: SPage = {
    columns: [
      {
        index: 'infoName',
        title: '姓名',
        filter: {
          placeholder:'请输入姓名',
          type: 'string'
        }
      },
     
      {
        index: 'mobile',
        title: '手机号码',
        filter: {
          index:'infoMobile',
          placeholder : '请输入手机号',
          type: 'string'
        }
      },
      {
        index: 'houseName',
        title: '区域名称',
        filter:{
          placeholder:"区域",
          type:"string"
        }
      }
    ],
    showCheckBox: false,
    reqUrl:this.api.manager.queryLndlordList.url,
    editBtn: [
      { title: '审批通过' , 
      iif: (data: STData) => {
        return data.approveStatus == 0
      } },
      { title: '拒绝审批' , 
      iif: (data: STData) => {
        return data.approveStatus == 0
      } },
      {title: '详情', iif: () => this.viewFlag},
      {title: '发卡', iif: () => this.cardFlag},
      {title: '编辑', iif: () => this.editFlag}
    ]
  }
  constructor(private http: _HttpClient,  private api: ApiService, private modal: NzModalService, private message: NzMessageService,private aclService: ACLService) { }
  
  ngOnInit() {

  }

  getBirthdayFromIdCard(idCard:any):string {
    var birthday = "";
    if(idCard != null && idCard != ""){
      if(idCard.length == 15){
        birthday = "19"+idCard.substr(6,6);
      } else if(idCard.length == 18){
        birthday = idCard.substr(6,8);
      }
      birthday = birthday.replace(/(.{4})(.{2})/,"$1-$2-");
    }
    return birthday;
  }

  operate(e: any): void {
    const _param=e.data;
    const type=0;
    if (e.title === '审批通过') {
      this.appliyPassBecomeLndlord({id:e.data.id,personId:e.data.personId});
    }else if(e.title === '拒绝审批'){
      this.appliyPassBecomeLndlord({id:e.data.id,approveStatus:"0",personId:e.data.personId});
    }else if(e.title === '详情'){
      const modal = this.modal.create({
        nzTitle: '详情',
        nzContent: LandlorEditComponent,
        nzWidth: 1200,
        nzComponentParams: {
          type,
          _param
        },
        nzOnOk: () => {
          this.st.reload();
        },
        nzFooter: null
      })
    }else if(e.title == "发卡"){
      const modal=this.modal.create({
        nzTitle:'发卡',
        nzContent:sendCardComponent,
        nzWidth:600,
        nzComponentParams:{
          _param
        },
        nzOnOk:()=>{
          this.st.reload();
        },
        nzFooter:null
      })
    }else if(e.title == '编辑'){
      const modal=this.modal.create({
        nzTitle:'编辑',
        nzContent:LandlorUpdateComponent,
        nzWidth:1200,
        nzComponentParams:{
          _param
        },
        nzOnOk:()=>{
          this.st.reload();
        },
        nzFooter:null
      })
    }
  }

  appliyPassBecomeLndlord(data){
    this.http.post('/core/manager/approveLndlord',data).subscribe(res => {
      if (res['status'] == '200') {
        this.message.success(res['message']);
        this.st.reload();
      }else {
        this.message.error(res['message'])
      }
    })
  }

  //新增房东
  addLandLord(){
    this.modal.create({
      nzTitle: "新增房东",
      nzContent: LandlordAddComponent,
      nzWidth: "70%",
      nzComponentParams: {
       
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
  }
  dataProcess(data: STData[]){
    console.log(data);
    return data;
  }
}
